package com.chen.web001;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
@WebServlet(urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("loginservlet----");
        String uname = req.getParameter("uname");
        String pass = req.getParameter("pass");

        if (uname != null && pass != null) {
            if (uname.equals("admin") && pass.equals("123456")) {
                req.getSession().setAttribute("username", uname);
                resp.sendRedirect("success.jsp");
            }else {
                //密码不对直接跳转http400页面
                resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
            }
        }
    }
}
